home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / utils / gemfut15.lzh / AESUTRC2.C < prev    next >
C/C++ Source or Header  |  1989-08-26  |  2KB  |  76 lines

  1.  
  2. /**************************************************************************
  3.  *
  4.  * AESFAST PD utilties.
  5.  *
  6.  *  Rectangle utilities 2...
  7.  *    rc_intersect
  8.  *************************************************************************/
  9.  
  10. #include <gemfast.h>
  11.  
  12. /*-------------------------------------------------------------------------
  13.  * rc_intersect - Compute intersection of 2 GRECT rectangles.
  14.  *                Returns TRUE if rectanlges have common area, FALSE if not.
  15.  *
  16.  *  (This may not be the best algorithm for doing this, but it seems to
  17.  *  work.  I basically stole it from the GEMQ PD bindings and modified
  18.  *  it a bit for performance. - Ian)
  19.  *-----------------------------------------------------------------------*/
  20.  
  21. int
  22. rc_intersect(prect1, prect2)
  23.     register GRECT *prect1;
  24.     register GRECT *prect2;
  25. {
  26.     register int    w1, w2;
  27.     int             lx, rx;
  28.     int             ty, by;
  29.     
  30.     /* calc right-side x as the lesser x of the two rectangles */
  31.      
  32.     w1 = prect1->g_x + prect2->g_w;
  33.     w2 = prect2->g_x + prect2->g_w;
  34.     rx  = (w1 < w2) ? w1 : w2;
  35.  
  36.     /*  calc bottom y as the lesser y of the two rectanlges */
  37.  
  38.     w1 = prect1->g_y + prect1->g_h;
  39.     w2 = prect2->g_y + prect2->g_h;
  40.     by  = (w1 < w2) ? w1 : w2;
  41.     
  42.     /* calc left-side x as the greater x of the two rectangles */
  43.  
  44.     w1 = prect1->g_x;
  45.     w2 = prect2->g_x;
  46.     lx = (w1 > w2) ? w1 : w2;
  47.     
  48.     /* calc top y as the greater y of the two rectangles */
  49.  
  50.     w1 = prect1->g_y;
  51.     w2 = prect2->g_y;
  52.     ty = (w1 > w2) ? w1 : w2;
  53.     
  54.     /* store the calculated rectangle (converting back to GRECT-type w/h) */
  55.  
  56.     prect2->g_x = lx;
  57.     prect2->g_y = ty;
  58.     prect2->g_w = rx - lx;
  59.     prect2->g_h = by - ty;
  60.  
  61.     /* 
  62.      * if the calculated width or height is negative, it indicates
  63.      * that there is no overlap in at least one dimension, and thus no
  64.      * overlap in the rectangles, so return FALSE.  if neither value is
  65.      * negative, there is a common intersecting area, so return TRUE.
  66.      */
  67.      
  68.     if ( (prect2->g_w < 0) || (prect2->g_h < 0) ) {
  69.         return 0;
  70.     }
  71.     else {
  72.         return 1;
  73.    }
  74. }
  75.  
  76.